﻿@inherits MasaComponentBase

<div class="full-height" style="padding:10px;">
    @if (ConfigurationRecord.IsEdit)
    {
        <MEditor @ref="_editor" Class="full-height" Value="@Content" ValueChanged="ContentChanged" Placeholder="请输入内容" ContentStyle="height:calc(100% - 95px);" BeforeAllUploadAsync="BeforeAllUploadAsync" />
    }
    else
    {
        @(new MarkupString(Content))
    }
</div>

@code {
    private MEditor _editor;

    [Inject]
    public IObjectStorageClient Client { get; set; } = default!;

    [Inject]
    private IJSRuntime JS { get; set; }

    [Inject]
    public IConfiguration Configuration { get; set; } = default!;

    public OssOptions OssOptions
    {
        get
        {
            return Configuration.GetSection("ConfigurationApi:public-$Config:$public.oss").Get<OssOptions>();
        }
    }

    [CascadingParameter]
    public ConfigurationRecord ConfigurationRecord { get; set; }

    [Parameter]
    public UpsertPanelDto Value { get; set; }

    [Parameter]
    public int MaximumFileSize { get; set; } = 1024 * 1024 * 2;

    string Content => Value[ExtensionFieldTypes.Content]?.ToString() ?? "";

    void ContentChanged(string content)
    {
        Value[ExtensionFieldTypes.Content] = content;
    }

    private async Task<bool> BeforeAllUploadAsync(List<EditorUploadFileItem> flist)
    {
        foreach (var item in flist)
        {
            if (item.Size > MaximumFileSize)
            {
                await PopupService.EnqueueSnackbarAsync($"{item.FileName} of {item.Size} bytes is larger than the limit of {MaximumFileSize} bytes", AlertTypes.Error);
                return false;
            }
        }
        var response = Client.GetSecurityToken();
        var stsToken = response.SessionToken;
        var accessId = response.AccessKeyId;
        var accessSecret = response.AccessKeySecret;
        var region = "oss-cn-hangzhou";
        var bucket = OssOptions.Bucket;
        var paramter = new SecurityTokenModel(region, accessId, accessSecret, stsToken, bucket);

        await JS.InvokeVoidAsync("EditorUploadImageToOss", _editor.ContentRef, _editor.Ref, paramter);
        return true;
    }
}
